<?php

/**
 * @package Joostina
 * @copyright Авторские права (C) 2008 Joostina team. Все права защищены.
 * @license Лицензия http://www.gnu.org/licenses/gpl-2.0.htm GNU/GPL, или help/license.php
 * Joostina! - свободное программное обеспечение распространяемое по условиям лицензии GNU/GPL
 * Для получения информации о используемых расширениях и замечаний об авторском праве, смотрите файл help/copyright.php.
 */
// запрет прямого доступа
defined('_VALID_MOS') or die();

require_once ($mainframe->getPath('front_html', 'com_content'));

class mod_boss_last_comments_Helper {

    var $_mainframe = null;

    function __construct($mainframe) {

        $this->_mainframe = $mainframe;
        mosMainFrame::addLib('text');
    }

    function get_items($params) {

        $database = $this->_mainframe->getDBO();
        $count = intval($params->get('count', 5));
        $catid = trim($params->get('catid'));

        $directory = $params->get('directory', 0);
        $comment_sys = $params->get('comment_sys', 'in');


        $whereCatid = '';
        if ($catid) {
            $catids = explode(',', $catid);
            mosArrayToInts($catids);
            $whereCatid = " AND ( category.id=" . implode(" OR category.id=", $catids) . " )";
        }
        //запрос для jComments
        if ($comment_sys == 'jc') {
            $query = "SELECT content.id as content_id, content.name as title,
		    content.comment as introtext, content.userid as created_by,
		    category.id as catid, content.date as created,
                    u.name AS author, u.name AS created_by_alias, u.usertype, u.username
                    FROM      #__jcomments AS content
                    LEFT JOIN #__boss_" . $directory . "_content_category_href as cat_href ON cat_href.content_id = content.id
                    LEFT JOIN #__boss_" . $directory . "_categories as category ON category.id = cat_href.category_id
                    LEFT JOIN #__users AS u ON u.id = content.userid

                    WHERE  content.published = 1 AND object_group = 'com_boss_" . $directory . "'"
                    . $whereCatid .
                    " GROUP BY content.id ORDER BY content.id DESC";
        }//запрос для внутренней системы комментирования
        else {
            $query = "SELECT content.contentid as content_id, content.title as title,
		    content.description as introtext, content.userid as created_by,
		    category.id as catid, content.date as created,
                    u.name AS author, u.name AS created_by_alias, u.usertype, u.username
                    FROM      #__boss_" . $directory . "_reviews AS content
                    LEFT JOIN #__boss_" . $directory . "_content_category_href as cat_href ON cat_href.content_id = content.contentid
                    LEFT JOIN #__boss_" . $directory . "_categories as category ON category.id = cat_href.category_id
                    LEFT JOIN #__users AS u ON u.id = content.userid

                    WHERE  content.published = 1 "
                    . $whereCatid .
                    " GROUP BY content.contentid ORDER BY content.id DESC";
        }



        $database->setQuery($query, 0, $count);
        $rows = $database->loadObjectList();

        return $rows;
    }

    function get_itemid($row, $params) {

        global $_SESSION;

        require_once( JPATH_BASE.'/components/com_boss/boss.tools.php' );

        $itemid = getBossItemid(intval($params->get('directory', 0)), intval($row->catid));

        return $itemid;
        
    }

    function prepare_row($row, $params) {

        $directory = intval($params->get('directory', 0));

        $row->Itemid_link = '';
        if ($params->get('def_itemid', '')) {
            $row->Itemid_link = '&amp;Itemid=' . $params->get('def_itemid');
        } else {
            $_itemid = $this->get_itemid($row, $params);
            if ($_itemid) {
                $row->Itemid_link = '&amp;Itemid=' . $_itemid;
            }
        }

        $row->link_on = sefRelToAbs('index.php?option=com_boss&amp;task=show_content&amp;contentid=' . $row->content_id . '&amp;catid=' . $row->catid . '&amp;directory=' . $directory . $row->Itemid_link);
        $row->link_text = $params->get('link_text', _READ_MORE);
        $readmore = ContentView::ReadMore($row, $params);

        $text = $row->introtext;
        $text = mosHTML::cleanText($text);
        if ($params->get('crop_text', 1)) {

            switch ($params->get('crop_text', 1)) {
                case 'simbol':
                default:
                    $text = Text::character_limiter($text, $params->get('text_limit', 250), '');
                    break;

                case 'word':
                    $text = Text::word_limiter($text, $params->get('text_limit', 25), '');
                    break;
            }
        }
        if ($params->get('text', 0) == 2) {
            $text = '<a href="' . $row->link_on . '">' . $text . '</a>';
        }

        $row->author = mosContent::Author($row, $params);
        $row->title = ContentView::Title($row, $params);
        $row->text = $text;
        $row->readmore = $readmore;

        return $row;
    }

}